<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ include file="/common/commonLib.jsp"%>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="renderer" content="webkit|ie-comp|ie-stand" />
		<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
		<title></title>

		<link rel="stylesheet" type="text/css" href="<c:url value='/css/personal-info.css'/>" />
		<link rel="stylesheet" type="text/css" href="<c:url value='/css/register.css'/>" />
		<script type="text/javascript" src="../js/md5.js"></script>
		<script type="text/javascript" src="../js/md51.js"></script>
<%--		<script type="text/javascript" src="../js/jquery.min.js"></script>--%>

	</head>

	<body>
		<div class="container-fluid">
			<div class="row">
				<div class="col-xs-12 info-title">
					<div class="col-xs-12 col-sm-6 col-md-6 col-lg-3 info-title-left">
						<shiro:hasRole name="teacher">
							<a href="${pageContext.request.contextPath}/course/list">精准微课堂</a>
						</shiro:hasRole>
						<shiro:hasRole name="assistant">
							<a href="${pageContext.request.contextPath}/accontrol/ajaxListCourseView">精准微课堂</a>
						</shiro:hasRole>
						<shiro:hasRole name="student">
							<a href="${pageContext.request.contextPath}/lesson/studentCLesson">精准微课堂</a>
						</shiro:hasRole>
					</div>
					<div class="col-xs-12 col-sm-6 col-md-6 col-lg-2 col-lg-push-7 info-title-right">
						<img src="<c:url value="/resource/picture/${image}" />" />${name}
					</div>
					<div class="col-xs-12 col-sm-12 col-md-12 col-lg-7 col-lg-pull-2 info-title-mid">
						
					</div>
				</div>
			</div>

			<div class="row">
				<div class="col-md-3 col-lg-4 col-sm-2"></div>
				<div class="col-md-6 col-lg-4 col-sm-8">
					<div class="info-title personal-info-title">
						个人信息修改
					</div><br />
					<div class="row">
						<div class="col-xs-1 col-sm-1"></div>
						<div class="col-md-12 col-xs-10 col-lg-12 col-sm-10">
							<form>
								<div class="form-group" style="display:none;">
									<div class="row">
										<div class="col-md-5 col-xs-6 col-sm-3">
											<label for="account">教工号/学号</label>
										</div>
										<div class="col-md-7 col-xs-10 col-sm-7">
											<input type="text" class="form-control" name="userCode" placeholder="教工号/学号">
										</div>
										<div class="col-xs-2 col-sm-2"></div>
									</div>
								</div>
								<div class="form-group">
									<div class="row">
										<div class="col-md-4 col-xs-6 col-sm-4">
											<label for="name">姓名</label>
										</div>
										<div class="col-md-6 col-xs-9 col-sm-6">
											<input type="text" class="form-control" name="name" placeholder="姓名">
										</div>
										<div class="col-md-2 col-xs-3 col-sm-2 warn">*必填</div>
									</div>

								</div>

								<div class="form-group">
									<div class="row">
										<div class="col-md-4 col-xs-6 col-sm-4">
											<label for="sex">性别</label>
										</div>
										<div class="col-md-6 col-xs-9 col-sm-6">
											<select id="gender" class="form-control">
												<option value="MALE">男</option>
												<option value="FEMALE">女</option>
											</select>
										</div>
										<div class="col-md-2 col-xs-3 col-sm-2 warn">*必填</div>
									</div>
								</div>
								<div class="form-group">
									<div class="row">
										<div class="col-md-4 col-xs-6 col-sm-4">
											<label for="phone">电话</label>
										</div>
										<div class="col-md-6 col-xs-9 col-sm-6">
											<input type="tel" class="form-control" name="mobilePhone" placeholder="电话">
										</div>
										<div class="col-md-2 col-xs-3 col-sm-2 warn">*必填</div>
									</div>
								</div>
								<div class="form-group">
									<div class="row">
										<div class="col-md-4 col-xs-6 col-sm-4">
											<label for="email">邮箱</label>
										</div>
										<div class="col-md-6 col-xs-10 col-sm-6">
											<input type="email" class="form-control" name="email" placeholder="邮箱">
										</div>
										<div class="col-md-2 col-xs-3 col-sm-2 warn">*必填</div>
									</div>
								</div>
								<div class="form-group">
									<div class="row">
										<div class="col-md-4 col-xs-6 col-sm-4">
											<label for="text">旧密码</label>
										</div>
										<div class="col-md-6 col-xs-9 col-sm-6">
											<input type="password" class="form-control" name="oldPassword"
												   placeholder="旧密码">
										</div>
										<div class="col-xs-2 col-sm-2"></div>
									</div>
								</div>
								<div class="form-group">
									<div class="row">
										<div class="col-md-4 col-xs-6 col-sm-4">
											<label for="text">新密码</label>
										</div>
										<div class="col-md-6 col-xs-9 col-sm-6">
											<input type="password" class="form-control" name="newPassword"
												   placeholder="新密码">
										</div>
										<div class="col-xs-2 col-sm-2"></div>
									</div>
								</div>
								<div class="form-group">
									<div class="row">
										<div class="col-md-4 col-xs-6 col-sm-4">
											<label for="qq">QQ</label>
										</div>
										<div class="col-md-6 col-xs-9 col-sm-6">
											<input type="text" class="form-control" name="qq" placeholder="QQ">
										</div>
									</div>
									<div class="col-xs-2 col-sm-2"></div>
								</div>
							</form>
						</div>
						<div class="col-xs-1 col-sm-1"></div>

					</div>
					<br />
					<div class="row">
						<div class="col-md-1 col-xs-1"></div>
						<div class="col-md-4 col-xs-3">
							<shiro:hasRole name="teacher">
								<button type="button" class="btn btn-default" onclick="javascript:window.location.href='${pageContext.request.contextPath}/course/list';">返回</button>
							</shiro:hasRole>
							<shiro:hasRole name="assistant">
								<button type="button" class="btn btn-default" onclick="javascript:window.location.href='${pageContext.request.contextPath}/accontrol/ajaxListCourseView';">返回</button>
							</shiro:hasRole>
							<shiro:hasRole name="student">
								<button type="button" class="btn btn-default" onclick="javascript:window.location.href='${pageContext.request.contextPath}/lesson/studentCLesson';">返回</button>
							</shiro:hasRole>
						</div>
						<div class="col-md-2 col-xs-2"></div>
						<div class="col-md-4 col-xs-3">
							<button type="submit" id="editPersonalInfo" class="btn btn-primary">修改</button>
						</div>
						<div class="col-md-1 col-xs-3"></div>
					</div>
					<br /><br />
				</div>
				<div class="col-md-3 col-lg-4 col-sm-2"></div>
			</div>
		</div>
		</div>
	</body>
<script>
var data=${data};

console.log(data)
var md5OldPassword = data.data.user.password;
var code = data.data.user.userCode;

$(document).ready(function(){
	
	$("#editPersonalInfo").on("click",editPersonalInfoEvent);  
	initPage(data.data.user);
})

var initPage=function(user){
	$("input[name='name']").val(user.name);
	$("input[name='classNumber']").val(user.classNumber);
	$("input[name='mobilePhone']").val(user.mobilePhone);
	var selections= document.getElementById("gender").options;
	var userGender = user.gender;
	for(var i=0;i<selections.length; i++){  
        var tempValue = selections[i].value;  
        if(tempValue == userGender)  
        {  
        	selections[i].selected = true;  
        }  
    }
	$("input[name='email']").val(user.email);
	$("input[name='qq']").val(user.qq);
}

var isNull = function(string){
	if(string == null || string.trim() == ""){
		return true;
	}else {
		return false;
	}
}


var editPersonalInfoEvent=function(){
	var id=${sessionScope.id};
	var userCode =$("input[name='userCode']").val();
	var oldPassword =$("input[name='oldPassword']").val();
	var newPassword =$("input[name='newPassword']").val();
	var name =$("input[name='name']").val();
	var classNumber =$("input[name='classNumber']").val();
	var mobilePhone =$("input[name='mobilePhone']").val();
	var email =$("input[name='email']").val();
	var qq =$("input[name='qq']").val();
	var gender =$("#gender  option:selected").val();
	var regph=/^[1][3,4,5,6,7,8][0-9]{9}$/;
	var password;
	var isCorrect = true;

	//电话号格式校验
	if(!regph.test(mobilePhone)){
		// layer.msg("手机号格式不正确")
		alert("手机号格式不正确！");
		return false;
	}

	//邮箱格式校验
	var regemail=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if(!regemail.test(email)){
		alert("邮箱格式不正确！");
		return false;
	}

	// //是否修改密码检验
	// if(!isCorrect(oldPassword)){
	// 	alert("旧密码错误！");
	// 	return false;
	// }

	var changePassword = false;
	if(isNull(name)||isNull(mobilePhone)||isNull(email)||isNull(gender)){
		alert("请完善信息！");
	}else {
        if(!isNull(oldPassword) && isNull(newPassword)){
			alert("请输入新密码！");
            window.location.reload();
            return false;
        }
        if(isNull(oldPassword) && !isNull(newPassword)){
			alert("请输入正确的旧密码！");
            window.location.reload();
            return false;
        }
		var json2 = {
			oldPassword: oldPassword,
			userCode: code,
			existPassword: md5OldPassword,
			newPassword: newPassword
		};

		console.log(json2)

		$.post("<c:url value="/password/checkOldPassword"/>", {
			data: JSON.stringify(json2),
		}, function (data) {
			if (data.status == "success") {
				console.log(data);
				if (isNull(oldPassword)){
				    password = md5OldPassword;
                } else if (data.message == "wrong"){
					alert("旧密码错误！请重新输入！");
                    window.location.reload();
                    isCorrect = false;
				} else {
					password = data.newPassword;
					console.log("changePassword1" + password);
					//alert("旧密码正确！已修改密码！");
				}
			} else {
				alert("wrong");
			}
		}).then(function (data) {
		    if(!isCorrect) {
                window.location.reload();
		        return false;
            }
			var json = {
				id: id,
				userCode: userCode,
				password: password,
				name: name,
				classNumber: classNumber,
				mobilePhone: mobilePhone,
				email: email,
				qq: qq,
				gender: gender
			}

			$.post("<c:url value='/user/update' />", {
				data : JSON.stringify(json),
			}, function(data) {
				if(data.status=="success"){
					alert("修改成功，重新登陆可生效！");
					window.location.href = CONTEXT_PATH + "user/logout";
				}else{
					alert("wrong");
				}
			});
		});

	}


	<%--if(changePassword){--%>
	<%--	console.log("changePasswordval" + changePassword);--%>
	<%--	console.log("changePassword2" + password);--%>
	<%--	var json = {--%>
	<%--		id: id,--%>
	<%--		userCode: userCode,--%>
	<%--		password: password,--%>
	<%--		name: name,--%>
	<%--		classNumber: classNumber,--%>
	<%--		mobilePhone: mobilePhone,--%>
	<%--		email: email,--%>
	<%--		qq: qq,--%>
	<%--		gender: gender--%>
	<%--	}--%>

	<%--	$.post("<c:url value='/user/update' />", {--%>
	<%--		data : JSON.stringify(json),--%>
	<%--		async: false--%>
	<%--	}, function(data) {--%>
	<%--		if(data.status=="success"){--%>
	<%--			alert("修改成功，重新登陆可生效！");--%>
	<%--			window.location.href = CONTEXT_PATH + "user/logout";--%>
	<%--		}else{--%>
	<%--			alert("wrong");--%>
	<%--		}--%>
	<%--	});--%>
	// }



}

</script>
</html>